<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Car Game</title>


    <style>
        body {
            margin: 0;
            padding: 0;
        }

        body .app {
            position: absolute;
            width: 100% !important;
            height: 100% !important;
            overflow: hidden;
            cursor: pointer;
        }

        body .app * {
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
        }

        body .app .background {
            background: url() no-repeat;
            background-size: cover;
            width: 100%;
            height: 100%;
        }

        body .app .logo {
            position: absolute;
            background: url() no-repeat;
            background-size: contain;
            width: 15%;
            height: 74px;
            top: 35px;
            left: 5%;
        }

        body .app .progress {
            position: absolute;
            top: 35px;
            right: 5%;
            width: 20%;
        }

        body .app .progress .progress-content {
            width: 100%;
            color: white;
            font-size: 20px;
            font-family: Arial, Helvetica, sans-serif;
            font-weight: bolder;
            line-height: 20px;
        }

        body .app .progress .progress-content .progress-item {
            position: relative;
            float: left;
            background-size: 100% auto !important;
            background-position: center !important;
            width: 18px;
            height: 18px;
        }

        body .app .progress .progress-content .progress-item.consumed {
            background: url() no-repeat;
        }

        body .app .progress .progress-content .progress-item.missed {
            background: url() no-repeat;
        }

        body .app .road-container {
            position: absolute;
            width: 50%;
            max-width: 156px;
            height: 100%;
            left: 0;
            right: 0;
            top: 0;
            margin: 0 auto;
            overflow: hidden;
        }

        body .app .road-container .road-content {
            position: absolute;
            width: 100%;
            height: 100%;
            top: 0;
            left: 0;
            right: 0;
            margin: 0 auto;
        }

        body .app .road-container .road-content .road {
            position: absolute;
            background: url() no-repeat;
            background-size: 100%;
            background-position: center;
            width: 100%;
            max-width: 156px;
            height: 600px;
        }

        body .app .road-container .road-energy {
            position: absolute;
            background: url() no-repeat;
            width: 35px;
            height: 24px;
            left: 92px;
        }

        body .app .road-container .road-energy.left {
            left: 28px;
        }

        body .app .road-container .road-energy.consumed {
            display: none;
        }

        body .app .car {
            position: absolute;
            background: url() no-repeat;
            width: 43px;
            height: 72px;
            top: 60%;
            -webkit-transition: left 0.4s;
            transition: left 0.4s;
        }

        body .app .arrow {
            position: absolute;
            width: 50px;
            height: 50px;
            bottom: 5vh;
            background: url() no-repeat;
            background-size: contain;
            background-repeat: no-repeat;
            cursor: pointer;
        }

        body .app .arrow.left-arrow {
            left: 5%;
            -webkit-transform: rotate(180deg);
            transform: rotate(180deg);
        }

        body .app .arrow.right-arrow {
            right: 5%;
        }

        body .app .start {
            position: absolute;
            background: url() no-repeat;
            width: 95px;
            height: 32px;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            margin: auto;
            cursor: pointer;
        }

        body .d-flex {
            display: -webkit-box;
            display: -ms-flexbox;
            display: flex;
        }

        body .align-items-center {
            -webkit-box-align: center;
            -ms-flex-align: center;
            align-items: center;
        }

        body .justify-content-end {
            -webkit-box-pack: end;
            -ms-flex-pack: end;
            justify-content: flex-end;
        }

        body .justify-content-center {
            -webkit-box-pack: center;
            -ms-flex-pack: center;
            justify-content: center;
        }

        body .padding-x-5 {
            padding-left: 5px;
            padding-right: 5px;
        }

        body .blink {
            -webkit-animation: blink-animation 1s linear infinite;
            animation: blink-animation 1s linear infinite;
        }

        @-webkit-keyframes blink-animation {
            50% {
                opacity: 0.5;
            }
        }

        @keyframes blink-animation {
            50% {
                opacity: 0.5;
            }
        }
    </style>

    <script>
        window.onload = () => {
            const startBtn = document.getElementById("start");
            const progress = document.getElementById("progress");
            const road = document.getElementById("road");
            const roadContainer = document.getElementById("roadContainer");
            const car = document.getElementById("car");
            const leftArrow = document.getElementById("leftArrow");
            const rightArrow = document.getElementById("rightArrow");
            const energyConsumedCount = document.getElementById("energyConsumed");
            const energyMissedCount = document.getElementById("energyMissed");


            const roadHeight = 580;
            const roadEnergyHeight = 24;
            const MIN_ACCELERATION = 1;
            const MAX_ACCELERATION = 5;
            const ACCELERATE_AFTER_EVERY = 3;

            let carTopPos = road.offsetHeight * .6;
            let acceleration = 1;
            let energyConsumed = 0;
            let energyMissed = 0;
            let gameOver = false;


            car.style.left = carSide(false) + 'px';

            window.onresize = function (event) {
                car.style.left = carSide(car.classList.contains("left")) + 'px';
                carTopPos = road.offsetHeight * .6;
            };

            initRoad();

            startBtn.addEventListener("click", () => {
                startBtn.style.display = 'none';

                startGame();
                updateProgress();

                initListeners();
            });

            function initListeners() {
                leftArrow.addEventListener("click", (e) => {
                    car.classList.add('left');
                    car.style.left = carSide(true) + 'px';
                });
                rightArrow.addEventListener("click", (e) => {
                    car.classList.remove('left');
                    car.style.left = carSide(false) + 'px';
                });
                document.onkeydown = (e) => {
                    if (e.keyCode == '37') {
                        car.classList.add('left');
                        car.style.left = carSide(true) + 'px';
                    }
                    else if (e.keyCode == '39') {
                        car.classList.remove('left');
                        car.style.left = carSide(false) + 'px';
                    }
                }
            }
            function initRoad() {
                addRoadWithEnergy(-roadHeight);
                addRoadWithEnergy(0);
                addRoadWithEnergy(roadHeight);
            }
            function addRoadWithEnergy(top) {
                const roadbg = document.createElement('div');
                roadbg.classList.add('road');
                roadbg.style.top = top + 'px';
                road.appendChild(roadbg);

                let energyTop = randomInt(roadEnergyHeight, roadHeight - roadEnergyHeight);
                let energyLeft = randomInt(1, 4) > 2;
                const roadEnergy = document.createElement('div');
                roadEnergy.classList.add('road-energy');
                roadEnergy.style.top = energyTop + 'px';
                if (energyLeft) roadEnergy.classList.add('left');
                roadbg.appendChild(roadEnergy);
            }
            function startGame() {
                setInterval(ticker, 5);
            }
            function ticker() {
                let roads = road.getElementsByClassName('road');
                for (let i = 0; i < roads.length; i++) {
                    let currentTop = parseFloat(roads[i].offsetTop);
                    let roadEnergy = roads[i].getElementsByClassName('road-energy')[0];

                    if (currentTop > roadHeight * 2) {
                        roads[i].style.top = (-roadHeight) + 'px';

                        let energyLeft = randomInt(1, 4) > 2;
                        if (energyLeft) roadEnergy.classList.add('left');
                        else roadEnergy.classList.remove('left');

                        roadEnergy.style.top = randomInt(roadEnergyHeight, roadHeight - roadEnergyHeight) + 'px';
                        roadEnergy.classList.remove('consumed');
                        roadEnergy.classList.remove('missed');
                    }
                    else {
                        roads[i].style.top = (currentTop + acceleration) + 'px';

                        let energyTop = currentTop + roadEnergy.offsetTop + acceleration;
                        if (carTopPos > energyTop &&
                            carTopPos < energyTop + roadEnergyHeight &&
                            !roadEnergy.classList.contains('consumed') && !roadEnergy.classList.contains('missed')) {
                            if (car.classList.contains('left') && roadEnergy.classList.contains('left') ||
                                !car.classList.contains('left') && !roadEnergy.classList.contains('left')) {
                                roadEnergy.classList.add('consumed');
                                energyConsumed++;
                            }
                            else {
                                roadEnergy.classList.add('missed');
                                energyMissed++;
                            }
                            updateProgress();
                        }

                    }
                }
            }
            function carSide(left) {
                return roadContainer.offsetLeft + road.offsetWidth * .1 + (road.offsetWidth * .8 / 2 * (left ? 1 : 3) - car.offsetWidth) / 2;
            }
            function updateProgress() {
                energyConsumedCount.innerHTML = energyConsumed;
                energyMissedCount.innerHTML = energyMissed;

                // acceleration
                acceleration = Math.min(Math.max(MIN_ACCELERATION, parseInt((energyConsumed - energyMissed) / ACCELERATE_AFTER_EVERY)), MAX_ACCELERATION);
            }


            function randomInt(min, max) {
                return Math.floor(Math.random() * (max - min)) + min;
            }
        }

    </script>
</head>

<body>

    <div class="app">
        <div class="background"></div>
        <div class="logo"></div>
        <div id="progress" class="progress">
            <div class="progress-content d-flex align-items-center justify-content-end">
                <div class="progress-item consumed"></div>
                <div class="padding-x-5">x</div>
                <div id="energyConsumed">0</div>
            </div>
            <div class="progress-content d-flex align-items-center justify-content-end">
                <div class="progress-item missed"></div>
                <div class="padding-x-5">x</div>
                <div id="energyMissed">0</div>
            </div>
        </div>
        <div id="roadContainer" class="road-container">
            <div id="road" class="road-content">
                <!-- <div class="road">
                    <div class="road-energy left" style="top: 222px;"></div>
                </div> -->
            </div>
        </div>
        <div id="car" class="car"></div>
        <div id="leftArrow" class="arrow left-arrow"></div>
        <div id="rightArrow" class="arrow right-arrow"></div>
        <div id="start" class="start blink"></div>
    </div>

</body>

</html>
